/*******************************************************************************

      ---------========= DSx86 Configuration Editor =========---------
                               by RyouArashi
                        http://ryouarashi.blogspot.com


              ---------========= License Agreement =========---------

Copyright 2010 RyouArashi. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY RyouArashi ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL RyouArashi OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
 * Utility Functions
 ******************************************************************************/

#include <conio.h>
#include <ctype.h>

#include "UTIL.H"

/* Funtions *******************************************************************/

/*
  Convert Hex to Integer
*/
unsigned HexToInt(char * String, unsigned * Result)
{
  unsigned k   = 4;
  unsigned ret = 0;

  /* Check Params */
  if (!String || !Result) return 0;

  /* Parse String */
  while (*String)
  {
    /* Parse no more than 4 digits */
    if (!k) return 0;

    /* Parse Digit */
    if ('0' <= *String && *String <= '9')
      ret = 16*ret + *String - '0';
    else if ('a' <= *String && *String <= 'f')
      ret = 16*ret + *String - 'a' + 10;
    else if ('A' <= *String && *String <= 'F')
      ret = 16*ret + *String - 'A' + 10;
    else
      return 0;

    /* Goto nex Digit */
    k--;
    String++;
  }

  /* Return */
  *Result = ret;
  return 1;
}

/*
  Get Key
*/
char GetKey()
{
  char c;

  /* Get Keyboard Input */
  c = getch();

  /* Value 0 == Special Key */
  if (c == 0)
  {
    /* Get Extended Key */
    c = getch();

    switch (c)
    {
      case 0x48: c = K_UP;    break;
      case 0x50: c = K_DOWN;  break;
      case 0x4B: c = K_LEFT;  break;
      case 0x4D: c = K_RIGHT; break;
      case 0x53: c = K_DEL;   break;
      default:   c = 0;       break;
    }
  }

  /* Return */
  return c;
}

/*
  Print at location
*/
void PrintAt(unsigned x, unsigned y, const char * s, unsigned Len, int Seletced)
{
    /* Text Color */
    if (Seletced)
    {
      /* Selected: White on Light Blue */
      textcolor(WHITE);
      textbackground(CYAN);
    }
    else
    {
      /* Normal: White on Black */
      textcolor(WHITE);
      textbackground(DARKGRAY);
    }

    /* Print */
    {
      unsigned l;
      gotoxy(x,y);
      l = cprintf("%s", s);

      while (l < Len) {cprintf(" "); l++; }
    }
}

/*
  Check if Profile Name is valid
*/
unsigned NameIsValid(char * s)
{
  unsigned j;
  unsigned len;

  /* check characters */
  len = strlen(s);
  if (len)
  {
    for (j = 0; j < len; j++)
    {
      if ('0' <= s[j] && s[j] <= '9');      /* Numbers are ok */
      else if ('a' <= s[j] && s[j] <= 'z'); /* Lower case is ok */
      else if ('A' <= s[j] && s[j] <= 'Z')  /* Upper case is autoconverted */
      {
        s[j] = tolower(s[j]);
      }
      else if ('_' == s[j]);                /* Underscore is ok */
      else                                  /* Anything else is invalid */
        return 0;
    }

    /* All ok */
    return 1;
  }

  else
  /* Empty Name */
  {
    return 0;
  }
}

/* String Compare Functions (not case sensitive) */
int nc_strcmp(const char * s1, const char * s2)
{
  while (*s1 || *s2)
  {
    int d;

    /* Compare */
    d = tolower(*s1) - tolower(*s2);

    /* Return on difference */
    if (d) return d;

    /* Next Char */
    s1++, s2++;
  }

  /* After loop both Chars are Zero, so strings are equal */
  return 0;
}

/* String Compare Functions (not case sensitive) */
int nc_strncmp(const char * s1, const char * s2, unsigned max)
{
  while (max && (*s1 || *s2))
  {
    int d;

    /* Compare */
    d = tolower(*s1) - tolower(*s2);

    /* Return on difference */
    if (d) return d;

    /* Next Char */
    s1++, s2++;
    max--;
  }

  /* After loop both Chars are Zero or max is Null, so strings are equal */
  return 0;
}

/* End of File ****************************************************************/